import java.util.LinkedList;

public class SeatsForSession extends Hall {
    private static LinkedList<SeatsForSession> seatsList = new LinkedList<>();

    private Session session;//场次
    private Movies movie;//影片
    private int totalSeats;//总座位数
    private int freeSeats;//剩余座位数
    private String[][] seatsInfo;//当前场次座位信息

    // 构造函数关联Session和Movie//！！！
    public SeatsForSession(Session session, Movies movie,Hall hall) {
        super(hall.getHallName(), hall.getRows(), hall.getCols());
        this.session = session;
        this.movie = movie;
        this.totalSeats =  hall.getRows() * hall.getCols();
        this.freeSeats = this.totalSeats;//初始化剩余座位数就是总座位数
        this.seatsInfo = new String[hall.getRows()][hall.getCols()];
        initializeSeats();
        seatsList.add(this);
    }

    // 初始化座位信息
    private void initializeSeats() {
        for (int i = 0; i < seatsInfo.length; i++) {
            for (int j = 0; j < seatsInfo[i].length; j++) {
                seatsInfo[i][j] = "0";
            }
        }
    }

    // 根据场次获取座位表
    public static SeatsForSession getSeatsBySession(Session targetSession) {
        //遍历所有场次
        for (SeatsForSession seats : seatsList) {
            //如果场次与目标场次相同，就返回座位
            if (seats.session.equals(targetSession)) {
                return seats;
            }
        }
        // 使用Session中的Hall对象创建座位表
        Hall hall = targetSession.getHall();
        return new SeatsForSession(targetSession, Movies.getMovieByName(targetSession.movieName), hall);
    }

    // 显示场次座位信息
    public void displaySeatsInfo() {
        System.out.println("\n=== 场次座位信息 ===");
        System.out.println("影片: " + session.movieName);
        System.out.println("影厅: " + session.getHallName());
        System.out.println("场次时间: " + session.getStartTime());
        System.out.println("总座位数: " + totalSeats);
        System.out.println("剩余座位: " + freeSeats);
        displaySeatMap();
    }

    // 显示座位图
    public void displaySeatMap() {
        System.out.println("\n=== 座位图 ===");
        System.out.print("   ");
        for (int i = 1; i <= seatsInfo[0].length; i++) {
            System.out.printf("%-4d", i);
        }
        System.out.println();

        for (int i = 0; i < seatsInfo.length; i++) {
            char row = (char) ('A' + i);
            System.out.print(row + "  ");
            for (int j = 0; j < seatsInfo[i].length; j++) {
                System.out.printf("%-4s", seatsInfo[i][j]);
            }
            System.out.println();
        }
        System.out.println("图例: [0]可预订 [1]已售出");
    }

    // 预订座位（相当于购买座位）
    public boolean bookSeat(String seatNumber) {
        int row = seatNumber.charAt(0) - 'A';
        int col = Integer.parseInt(seatNumber.substring(1)) - 1;

        if (row < 0 || row >= this.getRows() || col < 0 || col >= this.getCols()) {
            System.out.println("无效的座位号！");
            return false;
        }
        // 修改条件判断逻辑
        if (seatsInfo[row][col].equals("0")) {  // 检查座位是否可用
            seatsInfo[row][col] = "1";  // 标记为已预订
            freeSeats--;
            return true;
        } else {
            System.out.println("该座位已被预订！");
            return false;
        }
    }

    // 取消预订（相当于退票）
    public boolean cancelSeat(String seatNumber) {
        int row = seatNumber.charAt(0) - 'A';
        int col = Integer.parseInt(seatNumber.substring(1)) - 1;
        if (row < 0 || row >= this.getRows() || col < 0 || col >= this.getCols()) {
            System.out.println("无效的座位号！");
            return false;
        }
        if (seatsInfo[row][col].equals("0")) {
            System.out.println("该座位未被预订！");
            return false;
        }
        seatsInfo[row][col] = "0";
        freeSeats++;
        return true;
    }

    // Add getter method for free seats
    public int getFreeSeats() {
        return freeSeats;
    }

    // Add getter method for total seats
    public int getTotalSeats() {
        return totalSeats;
    }

}

